package edu.java.lectures.lec03.design.patterns;

import java.awt.Color;

// creates VALID object from a given type
// usually defines factory as Singleton
public class CarFactory {

    // --- Class Variables -----------------------------------------------------

    private static CarFactory instance; // part of the singleton idea

    private static long factoryId;

    // --- Constructors --------------------------------------------------------

    private CarFactory() {
	factoryId = 0;
    }

    // --- Methods (Singleton) -------------------------------------------------

    public static CarFactory instance() {
	if (instance == null) {
	    instance = new CarFactory();
	}
	return instance;
    }

    // --- Methods (Factory Methods) -------------------------------------------

    // returns object with VALID values
    public Car createDefaultCar() {
	factoryId++;

	Car car = new Car();
	car.setFactoryId(factoryId);
	car.setColor(Color.BLACK);
	car.setWeelsNumber(Car.MIN_WEELS_NUMBER);
	car.setReservoirVolume(Car.MIN_RESERVOIR_VOLUME);

	return car;
    }

    public Car createCar(int weelsNumber, Color color, double reservoirVolume) {
	if (weelsNumber <= Car.MIN_WEELS_NUMBER) {
	    throw new IllegalArgumentException("Invalid weel cars number: "
		    + weelsNumber);
	}
	if (color == null) {
	    throw new IllegalArgumentException("Invalid color: null.");
	}
	if (reservoirVolume < Car.MIN_RESERVOIR_VOLUME) {
	    throw new IllegalArgumentException("Invalid reservoir volume: "
		    + reservoirVolume);
	}
	factoryId++;

	Car car = new Car(factoryId, weelsNumber, color, reservoirVolume);

	return car;
    }

    public Car createCar2(int weelsNumber, Color color, double reservoirVolume) {
	factoryId++;

	Car car = new Car();
	car.setFactoryId(factoryId);
	car.setColor(color);
	car.setWeelsNumber(weelsNumber);
	car.setReservoirVolume(reservoirVolume);

	return car;
    }

}
